package cn.tinkling.views;

import java.io.UnsupportedEncodingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;

import cn.tinkling.TinklingResourceManager;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class UrlDecoder extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "cn.tinkling.views.UrlDecoder";
	private StyledText srcText;
	private StyledText resultText;

	/**
	 * The constructor.
	 */
	public UrlDecoder() {
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parent.setBackground(TinklingResourceManager.getDefaultBackground());
		GridLayout gl_parent = new GridLayout(1, false);
		gl_parent.verticalSpacing = 8;
		parent.setLayout(gl_parent);

		srcText = new StyledText(parent, SWT.BORDER | SWT.SINGLE);
		srcText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		srcText.setRightMargin(2);
		srcText.setLeftMargin(2);
		srcText.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
		srcText.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		srcText.setWordWrap(true);
		srcText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		Menu menu = new Menu(srcText);
		srcText.setMenu(menu);

		MenuItem menuItemPaste = new MenuItem(menu, SWT.NONE);
		menuItemPaste.setText("\u7C98\u8D34");
		menuItemPaste.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				srcText.setText("");
				srcText.paste();
			}
		});

		Button btnDecode = new Button(parent, SWT.NONE);
		btnDecode.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BORDER));
		btnDecode.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		GridData gd_btnDecode = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_btnDecode.widthHint = 135;
		btnDecode.setLayoutData(gd_btnDecode);
		btnDecode.setText("\u89E3    \u7801");

		resultText = new StyledText(parent, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		resultText.setAlwaysShowScrollBars(false);
		resultText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		resultText.setForeground(SWTResourceManager.getColor(51, 51, 51));
		resultText.setBottomMargin(2);
		resultText.setLeftMargin(2);
		resultText.setRightMargin(2);
		resultText.setTopMargin(2);
		resultText.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.NORMAL));
		resultText.setWordWrap(true);
		resultText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		btnDecode.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String result;
				try {
					String src = srcText.getText();
					result = java.net.URLDecoder.decode(src, "UTF-8");
					result = decode(result);
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
					result = e1.getMessage();
				}

				resultText.setText(result);
			}
		});
	}

	@Override
	public void setFocus() {
		srcText.setFocus();
	}

	public static String decode(String unicodeStr) {
		if (unicodeStr == null) {
			return null;
		}
		StringBuffer retBuf = new StringBuffer();
		int maxLoop = unicodeStr.length();
		for (int i = 0; i < maxLoop; i++) {
			if (unicodeStr.charAt(i) == '\\') {
				if ((i < maxLoop - 5) && ((unicodeStr.charAt(i + 1) == 'u') || (unicodeStr.charAt(i + 1) == 'U')))
					try {
						retBuf.append((char) Integer.parseInt(unicodeStr.substring(i + 2, i + 6), 16));
						i += 5;
					} catch (NumberFormatException localNumberFormatException) {
						retBuf.append(unicodeStr.charAt(i));
					}
				else
					retBuf.append(unicodeStr.charAt(i));
			} else {
				retBuf.append(unicodeStr.charAt(i));
			}
		}
		return retBuf.toString();
	}
}